// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <cantor/parser/parser.h>
#include <cantor/transform/exprs/algorithm_opt.h>
#include <cantor/transform/exprs/constant_expr.h>
#include <cantor/transform/expr.h>

namespace cantor::transform {

    void TransAlgorithmOpt::resolve_ast(const cantor::parser::AlgorithmOpt*node) {
        if (!node) {
            status = turbo::data_loss_error("no expr");
            return;
        }

        algorithm_opt.mutable_algo()->Clear();
        if (node->algo) {
            TransConstantExpr cexpr;
            cexpr.resolve_ast(node->algo);
            status = cexpr.check();
            if (!status.ok()) {
                return;
            }
            *algorithm_opt.mutable_algo() = std::move(cexpr.const_expr);
        }

        set_proto_location(node, algorithm_opt.mutable_location());
        set_proto_status(algorithm_opt.mutable_status());
    }

    const turbo::Status &TransAlgorithmOpt::check() {
        if (!status.ok()) {
            return status;
        }
        if (!algorithm_opt.has_location()) {
            make_missing_required_field("location");
        }
        return status;
    }
    turbo::Status TransAlgorithmOpt::resolve_to(turbo::Nonnull<const cantor::parser::AlgorithmOpt*> node, turbo::Nonnull<pb::ast::AlgorithmOpt*>out) {
        TransAlgorithmOpt translator;
        translator.resolve_ast(node);
        auto status = translator.check();
        if (!status.ok()) {
            return status;
        }
        *out = std::move(translator.algorithm_opt);
        return status;
    }
}  // namespace cantor::transform
